package com.bjesc_b2c.app.appraiset.widget.dialog;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.http.IApiActivityRequest;
import com.bjesc_b2c.app.appraiset.widget.BaseDialog;
import com.rxretrofit_build.util.CustomLog;
import com.rxretrofit_build.util.MapUtil;

import java.util.HashMap;

import master_bean.DealerBean;
import rxretrofit_base.model.ShowType;
import rxretrofit_base.model.TokenBean;
import rxretrofit_base.net.BaseSubscriber;
import rxretrofit_base.net.RxRequest;

/**
 * Created by Jason Chen on 2017/2/23.
 */

public class AnalyseDialog extends BaseDialog implements View.OnClickListener {
    private TextView analyseTipTv;
    private String vin = "";
    private DealerBean dealerBean;
    public static final int TYPE_VIN = 1; //检测vin
    public static final int TYPE_DEALER = 2; //添加车商
    private int type;
    private DealerDateListener listener;

    public AnalyseDialog(Context context, String vin, int type) {
        super(context);
        this.vin = vin;
        this.type = type;
        initViews();
    }

    public AnalyseDialog(Context context, DealerBean dealerBean, int type, DealerDateListener listener) {
        super(context);
        this.dealerBean = dealerBean;
        this.type = type;
        this.listener = listener;
        initViews();
    }

    private void initViews() {
        view = inflater.inflate(R.layout.dialog_analyse, null);
        view.findViewById(R.id.tv_sure).setOnClickListener(this);
        analyseTipTv = (TextView) view.findViewById(R.id.tv_analyse_tip);
        setContentView(view);
        requestedSort();
    }

    private void requestedSort() {
        if (type == TYPE_VIN) {
            HashMap<String, Object> buile = new MapUtil()
                    .put("vin", vin)
                    .build();
            RxRequest.request(ShowType.SHOW, RxRequest.getClient(getContext(), buile).create(IApiActivityRequest.class).sortVIN()
                    , new BaseSubscriber<TokenBean>(getContext()) {
                        @Override
                        public void onSuccess(TokenBean o) {
                            CustomLog.d("code = " + o.getStatus_code());
                            if (o.getStatus_code() == 0) {
                                analyseTipTv.setText("车源未被录入，请录入!");
                            } else {
                                analyseTipTv.setText("车源已存在!");
                            }
                            show();
                        }

                        @Override
                        public void onFailure(Throwable e) {

                        }
                    });
        } else if (type == TYPE_DEALER) { //添加车商

            HashMap<String, Object> build = new MapUtil()
                    .put("dealerName", dealerBean.getDealerName())
                    .put("boss", dealerBean.getBoss())
                    .put("phone", dealerBean.getPhone())
                    .put("market_id", dealerBean.getMarket_id())
                    .put("floor", dealerBean.getFloor())
                    .put("house_number", dealerBean.getHouse_number())
                    .put("car_number", dealerBean.getCar_number())
                    .put("month_sale", dealerBean.getMonth_sale())
                    .put("financial_penetration", dealerBean.getFinancial_penetration())
                    .put("financial_channel", dealerBean.getFinancial_channel())
                    .put("kan_che", dealerBean.getKan_che())
                    .put("xin_che", dealerBean.getXin_che())
                    .put("car_preference", dealerBean.getCar_preference())
                    .put("car_channel", dealerBean.getCar_channel())
                    .put("merchant_analysis", dealerBean.getMerchant_analysis())
                    .put("description", dealerBean.getDescription()).build();

            RxRequest.request(ShowType.SHOW, RxRequest.getClient(getContext(), build).create(IApiActivityRequest.class).addDealer()
                    , new BaseSubscriber<TokenBean<DealerBean>>(getContext()) {
                        @Override
                        public void onSuccess(TokenBean<DealerBean> o) {
                            if (o.getStatus_code() == 0) {
                                analyseTipTv.setText("车商添加成功！");
                                if (listener != null) {
                                    listener.getDealerDate(o.getData().getId());//"29,是固定格式表示车行车市"
                                }
                            } else {
                                analyseTipTv.setText("车商添加失败！");
                            }
                            show();
                        }

                        @Override
                        public void onFailure(Throwable e) {
                            analyseTipTv.setText("车商添加失败！");
                        }
                    });

        }
    }


    @Override
    public void onClick(View v) {
        dismiss();
    }

    public interface DealerDateListener {
        void getDealerDate(String id);

    }
}
